package com.library.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.library.dao.BorrowRecordDAO;
import com.library.entity.BorrowRecord;

/**
 * 管理员借阅管理Servlet
 */
@WebServlet("/admin/borrow")
public class AdminBorrowServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        // 设置请求和响应的字符编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        
        String action = request.getParameter("action");
        
        if (action == null) {
            // 默认显示所有借阅记录
            showAllRecords(request, response);
        } else if (action.equals("overdue")) {
            // 显示逾期未还记录
            showOverdueRecords(request, response);
        } else if (action.equals("return")) {
            // 管理员归还图书
            adminReturnBook(request, response);
        } else {
            // 默认显示所有借阅记录
            showAllRecords(request, response);
        }
    }
    
    // 显示所有借阅记录
    private void showAllRecords(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        BorrowRecordDAO recordDAO = new BorrowRecordDAO();
        List<BorrowRecord> records = recordDAO.getAllRecords();
        
        request.setAttribute("records", records);
        request.getRequestDispatcher("/admin/borrow_records.jsp").forward(request, response);
    }
    
    // 显示逾期未还记录
    private void showOverdueRecords(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        BorrowRecordDAO recordDAO = new BorrowRecordDAO();
        List<BorrowRecord> records = recordDAO.getOverdueRecords();
        
        request.setAttribute("records", records);
        request.getRequestDispatcher("/admin/overdue_records.jsp").forward(request, response);
    }
    
    // 管理员归还图书
    private void adminReturnBook(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        int recordId = Integer.parseInt(request.getParameter("recordId"));
        
        // 获取借阅记录
        BorrowRecordDAO recordDAO = new BorrowRecordDAO();
        BorrowRecord record = recordDAO.getRecordById(recordId);
        
        if (record == null) {
            // 借阅记录不存在
            request.setAttribute("error", "借阅记录不存在");
            response.sendRedirect("borrow");
            return;
        }
        
        if (record.getStatus() != 0) {
            // 图书已归还
            request.setAttribute("error", "该图书已归还");
            response.sendRedirect("borrow");
            return;
        }
        
        // 更新借阅记录状态
        boolean recordUpdated = recordDAO.returnBook(recordId);
        
        if (recordUpdated) {
            // 归还成功
            request.setAttribute("message", "图书归还成功");
            response.sendRedirect("borrow");
        } else {
            // 更新借阅记录失败
            request.setAttribute("error", "图书归还失败，请稍后再试");
            response.sendRedirect("borrow");
        }
    }
   
}
